using Godot;
using System;
using System.Collections.Generic;

public partial class AnimationPlay : Node2D,IComponent
{
	// Called when the node enters the scene tree for the first time

    private Characters _character;
    public AnimatedSprite2D _animated;
    private Texture2D _texture;
    private SpriteFrames _spriteFrame;
    
    private int y0 = -1;
    public void Initialize()
    {
        
        
    }

    public void Player_Animation(double direction , string action ,float scale)//播放动画，根据方向调整
    {

        _character = GetParent<Characters>();
        
        
        if (_character != null)  
        {  
            
            _animated = _character.GetNode<AnimatedSprite2D>("AnimatedSprite2D");
                    //GD.Print("同级节点的名称是: " + child.Name);  
                    
            //_spriteFrame.AddAnimation("default");//创建动画帧
               
        }  
        else  
        {  
            GD.Print("该节点没有父节点，因此也没有同级节点");  
        }

        var path = "res://assets/textures/";
        path = path + _character.Name + "/" + _character.Name + "_" +action + ".png";
        path = path.ToLower();
        //GD.Print(path);
        _texture = GD.Load(path) as Texture2D;
        _spriteFrame = new SpriteFrames(); 
        int x,y;
        var _cellsize = new Vector2I((int)_texture.GetSize().X / 27,(int)_texture.GetSize().Y / 8);//计算最小单元
        _cellsize = new Vector2I(96,96);
        
        if(direction>=22.5&&direction<22.5+45)
		{y=6;}
		else if(direction>=22.5+45&&direction<22.5+45*2)
		{y=7;}
		else if(direction>=22.5*2+45&&direction<22.5+45*3)
		{y=8;}
		else if(direction>=22.5*3+45&&direction<22.5+45*4)
		{y=1;}
		else if(direction>=22.5*4+45&&direction<22.5+45*5)
		{y=2;}
		else if(direction>=22.5*5+45&&direction<22.5+45*6)
		{y=3;}
		else if(direction>=22.5*6+45&&direction<22.5+45*7)
		{y=4;}
		else
		{y=5;}
        
        if(action != _character.CharacterData.state||y != y0)
        {
            _spriteFrame.Clear("default");
            int x_size = (int)_texture.GetSize().X/_cellsize.X;
            
            for(x = 0;x < x_size;x++)
            {
                var frame = new AtlasTexture();
                frame.Atlas = _texture;
                frame.Region = new Rect2I(new Vector2I(x,y-1) * _cellsize,_cellsize);//裁剪动画帧
                _spriteFrame.AddFrame("default",frame);
            }
            _character.CharacterData.state = action;
            y0 = y;
        
            _animated.SpriteFrames = _spriteFrame;
            //GD.Print("更新动画帧");
            
        }
        if(action == "attack")
        {
            _spriteFrame.SetAnimationLoop("default",false);
        }
        else
        {
            _spriteFrame.SetAnimationLoop("default",true);
        }
        _animated.Play("default",scale);
        //GD.Print(_animated.GetPlayingSpeed());
        //GD.Print(_animated.Frame);
        //GD.Print(y);
        
        
    }

    public void Update(double delta)
    {
    }

    public void AnimationStop()
    {
        _animated.Stop();
    }
}
